"""A collection of all commands that a Kanna can use to interact with the game."""

from src.common import config, settings, utils
import time
import math
from src.routine.components import Command
from src.common.vkeys import press, key_down, key_up
from src.command_book.command_book import BaseKey

# List of key mappings
class Key(BaseKey):
    # Movement

    龍脈讀取 = 'd'

    # Skills
    技能1 = 'a'
    技能2 = 's'
    技能3 = 'd'
    技能4 = 'f'
    技能5 = 'g'
    技能6 = 'q'
    技能7 = 'w'
    技能8 = 'e'
    技能9 = 'r'
    技能10 = 'e'


#########################
#       Commands        #
#########################
def step(direction, target):
    """
    Performs one movement step in the given DIRECTION towards TARGET.
    Should not press any arrow keys, as those are handled by Auto Maple.
    """

    num_presses = 2
    if direction == 'up' or direction == 'down':
        num_presses = 1
    if config.stage_fright and direction != 'up' and utils.bernoulli(0.75):
        time.sleep(utils.rand_float(0.1, 1))
    d_y = target[1] - config.player_pos[1]
    # print('目标点：{}------自己位置：{} = ：{}'.format(target[1],config.player_pos[1],d_y))
    if abs(d_y) > settings.move_tolerance * 1.5:
        if direction == 'down':
            key_down(direction)
            press(Key.JUMP, 3)
            key_up(direction)
        elif direction == 'up':
            # print('222')
            press('s', 2)
            # press('a', 1)
    if direction == 'right':
        key_down(direction)
        press(Key.JUMP, 3)
        key_up(direction)
        # press('a', 1, down_time=0.1, up_time=0.2)
    elif direction == 'left':
        key_down(direction)
        press(Key.JUMP, 3)
        key_up(direction)
        # press('a', 1, down_time=0.1, up_time=0.2)
    # time.sleep(utils.rand_float(0.1, 0.5))

    ranInt = utils.rand_int(1,2)
    # print(f'{ranInt}')
    # if ranInt == 1:
    #     press('left', 1, down_time=0.1, up_time=0.05)
    #     press('d', 1)
    # if ranInt == 2:
    #     press('right', 1, down_time=0.1, up_time=0.05)
    #     press('d', 1)
    #
    # time.sleep(utils.rand_float(0.3, 0.5))
    press(Key.主攻, 1,down_time=0.1)
    # time.sleep(utils.rand_float(0.1, 0.25))
    press(Key.龍脈讀取, 1, down_time=0.1)
    # randomSkill().main()
    # print('--------------------------------')



class Adjust(Command):
    """Fine-tunes player position using small movements."""

    def __init__(self, x, y, max_steps=5):
        super().__init__(locals())
        self.target = (float(x), float(y))
        self.max_steps = settings.validate_nonnegative_int(max_steps)

    def main(self):
        counter = self.max_steps
        toggle = True
        error = utils.distance(config.player_pos, self.target)
        while config.enabled and counter > 0 and error > settings.adjust_tolerance:
            if toggle:
                d_x = self.target[0] - config.player_pos[0]
                threshold = settings.adjust_tolerance / math.sqrt(2)
                if abs(d_x) > threshold:
                    walk_counter = 0
                    if d_x < 0:
                        key_down('left')
                        while config.enabled and d_x < -1 * threshold and walk_counter < 60:
                            time.sleep(0.05)
                            walk_counter += 1
                            d_x = self.target[0] - config.player_pos[0]
                        key_up('left')
                    else:
                        key_down('right')
                        while config.enabled and d_x > threshold and walk_counter < 60:
                            time.sleep(0.05)
                            walk_counter += 1
                            d_x = self.target[0] - config.player_pos[0]
                        key_up('right')
                    counter -= 1
            else:
                d_y = self.target[1] - config.player_pos[1]
                if abs(d_y) > settings.adjust_tolerance / math.sqrt(2):
                    if d_y < 0:
                        Teleport('up').main()
                    else:
                        key_down('down')
                        time.sleep(0.05)
                        press(Key.JUMP, 3, down_time=0.1)
                        key_up('down')
                        time.sleep(0.05)
                    counter -= 1
            error = utils.distance(config.player_pos, self.target)
            toggle = not toggle


class Buff(Command):
    """Uses each of Kanna's buffs once. Uses 'Haku Reborn' whenever it is available."""

    def __init__(self):
        super().__init__(locals())
        self.buff_time = 0

    def main(self):
        buffs = ['f1', 'f2', 'f3', 'f4', 'f5']
#         buffs = [Key.SPEED_INFUSION, Key.HOLY_SYMBOL,Key.HAKU,Key.AKATSUKI_WARRIOR]
#         now = time.time()
#         if self.huanxiang_time == 0 or now - self.huanxiang_time > 180:
#             press('1', 2)
#             self.huanxiang_time = now
#         if self.kongjianzhan_time == 0 or now - self.kongjianzhan_time > 60:
#             press('2', 2)
#             self.kongjianzhan_time = now
#         if self.ranxiaojian_time == 0 or now - self.ranxiaojian_time > 21:
#             press('3', 2)
#             self.ranxiaojian_time = now
#         if self.kill_4 == 0 or now - self.kill_4 > 30:
#             press('4', 2)
#             self.kill_4 = now
#         if self.kill_5 == 0 or now - self.kill_5 > 181:
#             press('5', 2)
#             self.kill_5 = now
#         if self.kill_6 == 0 or now - self.kill_6 > 181:
#             press('6', 2)
#             self.kill_6 = now
#         if self.kill_7 == 0 or now - self.kill_7 > 400:
#             press('7', 2)
#             self.kill_7 = now
#         if self.buff_time == 0 or now - self.buff_time > settings.buff_cooldown:
#             for key in buffs:
#                 press(key, 2)
#             self.buff_time = now


class Teleport(Command):
    """
    Teleports in a given direction, jumping if specified. Adds the player's position
    to the current Layout if necessary.
    """

    def __init__(self, direction, jump='False'):
        super().__init__(locals())
        self.direction = settings.validate_arrows(direction)
        self.jump = settings.validate_boolean(jump)

    def main(self):
        num_presses = 3
        time.sleep(0.05)
        if self.direction in ['up', 'down']:
            num_presses = 2
        if self.direction != 'up':
            key_down(self.direction)
            time.sleep(0.05)
        if self.jump:
            if self.direction == 'down':
                press(Key.JUMP, 3, down_time=0.1)
            else:
                press(Key.JUMP, 1)
        if self.direction == 'up':
            key_down(self.direction)
            time.sleep(0.05)
        press(Key.钩子, num_presses)
        key_up(self.direction)
        if settings.record_layout:
            config.layout.add(*config.player_pos)


class 技能1(Command):
    """Uses 'Tengu Strike' once."""

    def __init__(self, duration=120, 按鍵次數=2):
        super().__init__(locals())
        self.duration = settings.validate_nonnegative_int(duration)
        self.time = 0
        self.按鍵次數 = settings.validate_nonnegative_int(按鍵次數)

    def main(self):
        kills = [Key.技能1]
        now = time.time()
        if self.time == 0 or now - self.time > self.duration:
            for key in kills:
                press(key, self.按鍵次數, up_time=0.3)
            self.time = now

class 技能2(Command):
    """Uses 'Tengu Strike' once."""

    def __init__(self, duration=120, 按鍵次數=2):
        super().__init__(locals())
        self.duration = settings.validate_nonnegative_int(duration)
        self.time = 0
        self.按鍵次數 = settings.validate_nonnegative_int(按鍵次數)

    def main(self):
        kills = [Key.技能2]
        now = time.time()
        if self.time == 0 or now - self.time > self.duration:
            for key in kills:
                press(key, self.按鍵次數, up_time=0.3)
            self.time = now


class 技能3(Command):
    """Uses 'Tengu Strike' once."""

    def __init__(self, duration=120, 按鍵次數=2):
        super().__init__(locals())
        self.duration = settings.validate_nonnegative_int(duration)
        self.time = 0
        self.按鍵次數 = settings.validate_nonnegative_int(按鍵次數)

    def main(self):
        kills = [Key.技能3]
        now = time.time()
        if self.time == 0 or now - self.time > self.duration:
            for key in kills:
                press(key, self.按鍵次數, up_time=0.3)
            self.time = now

class 技能4(Command):
    """Uses 'Tengu Strike' once."""

    def __init__(self, duration=120, 按鍵次數=2):
        super().__init__(locals())
        self.duration = settings.validate_nonnegative_int(duration)
        self.time = 0
        self.按鍵次數 = settings.validate_nonnegative_int(按鍵次數)

    def main(self):
        kills = [Key.技能4]
        now = time.time()
        if self.time == 0 or now - self.time > self.duration:
            for key in kills:
                press(key, self.按鍵次數, up_time=0.3)
            self.time = now

class 技能5(Command):
    """Uses 'Tengu Strike' once."""

    def __init__(self, duration=120, 按鍵次數=2):
        super().__init__(locals())
        self.duration = settings.validate_nonnegative_int(duration)
        self.time = 0
        self.按鍵次數 = settings.validate_nonnegative_int(按鍵次數)

    def main(self):
        kills = [Key.技能5]
        now = time.time()
        if self.time == 0 or now - self.time > self.duration:
            for key in kills:
                press(key, self.按鍵次數, up_time=0.3)
            self.time = now

class 技能6(Command):
    """Uses 'Tengu Strike' once."""

    def __init__(self, duration=120, 按鍵次數=2):
        super().__init__(locals())
        self.duration = settings.validate_nonnegative_int(duration)
        self.time = 0
        self.按鍵次數 = settings.validate_nonnegative_int(按鍵次數)

    def main(self):
        kills = [Key.技能6]
        now = time.time()
        if self.time == 0 or now - self.time > self.duration:
            for key in kills:
                press(key, self.按鍵次數, up_time=0.3)
            self.time = now

class 技能7(Command):
    """Uses 'Tengu Strike' once."""

    def __init__(self, duration=120, 按鍵次數=2):
        super().__init__(locals())
        self.duration = settings.validate_nonnegative_int(duration)
        self.time = 0
        self.按鍵次數 = settings.validate_nonnegative_int(按鍵次數)

    def main(self):
        kills = [Key.技能7]
        now = time.time()
        if self.time == 0 or now - self.time > self.duration:
            for key in kills:
                press(key, self.按鍵次數, up_time=0.3)
            self.time = now

class 技能8(Command):
    """Uses 'Tengu Strike' once."""

    def __init__(self, duration=120, 按鍵次數=2):
        super().__init__(locals())
        self.duration = settings.validate_nonnegative_int(duration)
        self.time = 0
        self.按鍵次數 = settings.validate_nonnegative_int(按鍵次數)

    def main(self):
        kills = [Key.技能8]
        now = time.time()
        if self.time == 0 or now - self.time > self.duration:
            for key in kills:
                press(key, self.按鍵次數, up_time=0.3)
            self.time = now
class 技能9(Command):
    """Uses 'Tengu Strike' once."""

    def __init__(self, duration=120, 按鍵次數=2):
        super().__init__(locals())
        self.duration = settings.validate_nonnegative_int(duration)
        self.time = 0
        self.按鍵次數 = settings.validate_nonnegative_int(按鍵次數)

    def main(self):
        kills = [Key.技能9]
        now = time.time()
        if self.time == 0 or now - self.time > self.duration:
            for key in kills:
                press(key, self.按鍵次數, up_time=0.3)
            self.time = now

class 技能10(Command):
    """Uses 'Tengu Strike' once."""

    def __init__(self, duration=120,按鍵次數=2):
        super().__init__(locals())
        self.duration = settings.validate_nonnegative_int(duration)
        self.time = 0
        self.按鍵次數 = settings.validate_nonnegative_int(按鍵次數)

    def main(self):
        kills = [Key.技能10]
        now = time.time()
        if self.time == 0 or now - self.time > self.duration:
            for key in kills:
                press(key, self.按鍵次數, up_time=0.3)
            self.time = now